<template>
  <div class="app-container home">
    <query-panel
      :exportSrr="true"
      @exportHand="exportHand"
      :form-data="queryParams"
      @handleQuery="handleQuery"
      :spacing="14"
    >
      <el-row :gutter="20">
        <el-col :span="12">
          <el-form-item
            label="方案名称："
            prop="proposalName"
            label-width="100px"
          >
            <el-input v-model="queryParams.proposalName" placeholder="请输入" />
          </el-form-item>
        </el-col>
        <el-col :span="12">
          <el-form-item label="编码：" prop="code" label-width="60px">
            <el-input v-model="queryParams.code" placeholder="请输入" />
          </el-form-item>
        </el-col>
        <!-- <el-col :span="12">
          <el-button @click="exportHand">导出</el-button>
        </el-col> -->
      </el-row>
    </query-panel>
    <table-panel
      ref="tablePanel"
      align="left"
      table-data="/maintain/maintain/proposalMg/list"
      :queryParams="queryParams"
      :columns="columns"
      :operateArray="operateArray"
      @onOperate="onOperate"
    >
      <template #link="{ row }">
        <div class="pointer" @click="handleDetails(row)">{{ row.code }}</div>
      </template>
    </table-panel>
    <el-dialog
      title="方案报价"
      :visible.sync="detailsDialogShow"
      width="1200px"
      v-if="detailsDialogShow"
    >
      <plan-details ref="planDetails" :row-data="rowData" />
    </el-dialog>
  </div>
</template>

<script>
import TablePanel from "@/components/TablePanel/index.vue";
import QueryPanel from "@/components/QueryPanel/index.vue";
import "@riophae/vue-treeselect/dist/vue-treeselect.css";
import { deletePlanList, planListDetails, seacrhDict } from "@/api/planManage/index.js";
import PlanDetails from "./src/PlanDetails.vue";
export default {
  name: "nodeIndex",
  components: {
    TablePanel,
    QueryPanel,
    PlanDetails
  },
  data() {
    return {
      queryParams: {
        proposalName: "",
        code: "",
      },
      operateArray: [
        {
          name: "删除",
          icon: "table-column-delete",
        },
      ],
      columns: [
        {
          field: "code",
          label: "编码",
          slots: { default: "link" },
        },
        {
          field: "proposalName",
          label: "方案名称",
        },
        {
          field: "userName",
          label: "录入人",
        },
        {
          field: "nodeName",
          label: "所属节点",
        },
        {
          field: "createTime",
          label: "创建时间",
          type: "date",
        },
        {
          label: "操作",
          width: "105px",
        },
      ],
      menuOptions: [],
      detailsDialogShow: false,
      rowData: []
    };
  },

  methods: {
    // 查询
    handleQuery() {
      this.$refs.tablePanel.getTable();
    },
    // 操作列回调
    onOperate(type, row) {
      this.$confirm("确认删除吗？").then(() => {
        deletePlanList(row.quoteProposalId)
          .then((res) => {
            this.$message.success("操作成功！");
            this.$refs.tablePanel.getTable();
          })
          .catch((err) => {
            this.$message.error("操作失败！");
          });
      });
    },
    // 详情页面回调
    handleDetails(row) {
      this.detailsDialogShow = true;
      this.rowData = row;
      planListDetails(this.rowData.quoteProposalId).then((res) => {
        this.$refs.planDetails.listData = [];
        this.$refs.planDetails.total = 0;
        this.$refs.planDetails.count = res.data.motorCarNum;
        const data = res.data.quote;
        data.forEach(item => {
          if (item.groupMethod === "1" && item.configurationType === "1") {
            item.sumDomesticGoodsPrice = item.nums * item.domesticGoodsPrice;
            item.sumFreeImportGoodsPrice = item.nums * item.freeImportGoodsPrice;
            item.sumNoFreeImportGoodsPrice = item.nums * item.noFreeImportGoodsPrice;
            item.total = item.sumDomesticGoodsPrice + item.sumFreeImportGoodsPrice + item.sumNoFreeImportGoodsPrice;
            this.$refs.planDetails.motorBase.push(item);
          } else if (item.groupMethod === "1" && item.configurationType === "2") {
            item.sumDomesticGoodsPrice = item.nums * item.domesticGoodsPrice;
            item.sumFreeImportGoodsPrice = item.nums * item.freeImportGoodsPrice;
            item.sumNoFreeImportGoodsPrice = item.nums * item.noFreeImportGoodsPrice;
            item.total = item.sumDomesticGoodsPrice + item.sumFreeImportGoodsPrice + item.sumNoFreeImportGoodsPrice;
            this.$refs.planDetails.motorChoose.push(item);
          } else if (item.groupMethod === "2" && item.configurationType === "1") {
            item.sumDomesticGoodsPrice = item.nums * item.domesticGoodsPrice;
            item.sumFreeImportGoodsPrice = item.nums * item.freeImportGoodsPrice;
            item.sumNoFreeImportGoodsPrice = item.nums * item.noFreeImportGoodsPrice;
            item.total = item.sumDomesticGoodsPrice + item.sumFreeImportGoodsPrice + item.sumNoFreeImportGoodsPrice;
            this.$refs.planDetails.trailerBase.push(item);
          } else if (item.groupMethod === "2" && item.configurationType === "2") {
            item.sumDomesticGoodsPrice = item.nums * item.domesticGoodsPrice;
            item.sumFreeImportGoodsPrice = item.nums * item.freeImportGoodsPrice;
            item.sumNoFreeImportGoodsPrice = item.nums * item.noFreeImportGoodsPrice;
            item.total = item.sumDomesticGoodsPrice + item.sumFreeImportGoodsPrice + item.sumNoFreeImportGoodsPrice;
            this.$refs.planDetails.trailerChoose.push(item);
          }
          this.$refs.planDetails.total += item.total;
        })
        this.$refs.planDetails.getCArList('motorCarNum');
        this.$refs.planDetails.getCArList('trailerNum');
      });
    },
    // 数据导出
    exportHand() {
      this.download('maintain/maintain/proposalMg/export', {
        ...this.queryParams
      }, `job_${new Date().getTime()}.xlsx`)
    }
  },
};
</script>

<style scoped lang="scss">
.pointer {
  font-size: 16px;
  color: #333333a6;
}
</style>
